import requests
from configs.config import Host
from lib.apilib.login import Login
from pprint import pprint
class Myshop:
    #1.需要token
    def __init__(self,intoken):
        self.header={'Authorization':intoken}

    #2.列出商铺
    def shop_list(self,indata):
        payload=indata
        url=f'{Host}/shopping/myShop'
        resp=requests.get(url,headers=self.header,params=payload)
        return resp.json()

    #3上传图片
    def file_upload(self,filename,filedir):
        '''
        :param filename:    文件名
        :param filedir:    文件路径
        :return:
        '''
        #user_file1={'file':('123.jpg',open('../../data/123.jpg','rb'),'image/jpg')}

        user_file={'file':(filename,open(filedir,'rb'))}
        resp=requests.post(f'{Host}/file',files=user_file,headers=self.header)
        return resp.json()['data']['realFileName']
    #4编辑商铺
    def shop_update(self,indata,shopid,imageinfo):
        '''

        :param indata:    正常参数
        :param shopid:    商铺id
        :param imageinfo: 图片信息
        :return:
        '''
        indata['id']=shopid #更新店铺的id
        indata['image_path'] = imageinfo #更新店铺的图片信息
        indata['image']=f'{Host}/file/getImgStream?fileName={imageinfo}'
        payload = indata
        url = f'{Host}/shopping/updatemyshop'
        resp = requests.post(url,headers=self.header, data=payload)
        return resp.json()



if __name__ == '__main__':
    #1登录
    token=Login().login({"username":"ma0348","password":"96168"},getToken=True)

    #2列出商铺， 获取第一个店铺的id
    result = Myshop(token).shop_list({"page": 1, "limit": 20})
    id=result['data']['records'][0]['id']

    #3文件上传,并获取已上传的图片的信息
    imageres=Myshop(token).file_upload('123.jpg','../../data/123.jpg')

    #4编辑商铺
    data={
            "name": "星巴克新建店",
            "address": "上海市静安区秣陵路303号",
            "id": "3269",
            "Phone": "13176876632",
            "rating": "6.0",
            "recent_order_num":100,
            "category": "快餐便当/简餐",
            "description": "满30减5，满60减8",
            "image_path": "b8be9abc-a85f-4b5b-ab13-52f48538f96c.png",
            "image": "http://121.41.14.39:8082/file/getImgStream?fileName=b8be9abc-a85f-4b5b-ab13-52f48538f96c.png"
        }
    pprint(Myshop(token).shop_update(data,id,imageres))
    #
